﻿/**
* ...
* @author alijayameilio
*/

package particling;

import flash.display.Sprite;
import flash.events.Event;
import particling.force.Force;

class Particle 
{

	public var container(default, null):Sprite;
	public var curAmount:Int;
	public var particles:List<AParticle>;
	
	public function new() 
	{
		container = new Sprite();
		curAmount = 0;
		particles = new List<AParticle>();
		container.addEventListener(Event.ENTER_FRAME, move);
	}
	
	private function move(e:Event)
	{
		if (container.parent != null)
		{
			for (p in particles)
			{
				var indexForce:Int = 0;
				p.changeForce(0, 0);
				while (true)
				{
					if (indexForce == p.particleEmitter.forceList.length)
					{
						break;
					}
					var curForce:Force = p.particleEmitter.forceList[indexForce];
					if (curForce.active == true)
					{
						p.addForce(curForce.getXForce(p.container.x, p.container.y), curForce.getYForce(p.container.x, p.container.y));
					}
					indexForce++;
				}
				//trace(p.xForce);
				p.addSpeed(p.xForce, p.yForce);
				p.container.x += p.xSpeed;
				p.container.y += p.ySpeed;
				//trace(p.xSpeed);
				//trace(p.container.x);
				if (p.rotationFollow == true)
				{
					var rotation:Float = Math.atan2(p.ySpeed, p.xSpeed) * 180 / Math.PI;
					p.container.rotation = rotation + 90;
				}
				if (p.lifeTime > 0)
				{
					p.lifeTime--;
				} else if (p.lifeTime == 0 && p.container.parent != null)
				{
					p.particleEmitter.killParticle(p);
				}
			}
		}
	}
	
}